<meta title="新增选品库" width="1000" args="itemSelection,shopcat,name,config"/>
<style>
</style>
<div class="p">
    <form s="valid,form,nda,datac"
          s-data="itemSelection"
          method="post"
          action="/api/itemSelection"
          id="form" class="form-inline">
        <table class="table table-bordered table-hover table-striped">
            <tr>
                <td style="width: 100px;">名称</td>
                <td>
                    <input autocomplete="off" type="text" style="width: 100%" name="name"
                           id="name" s-valid-error="require()" class="form-control" placeholder="名称">
                </td>
            </tr>
            <tr>
                <td>频道</td>
                <td>
                    <select class="form-control" s="select" s-build="S.get('/api/channel/list')" name="channelId">
                        <option value="">不选频道</option>
                    </select>
                    <span>自动清空</span>
                    <label class="s-checkbox-switch">
                        <input autocomplete="off" type="checkbox" name="clearChannelItem" value="true" />
                        <span class="s-checkbox-indicator"></span>
                    </label>
                </td>
            </tr>
            <tr>
                <td>类目</td>
                <td>
                    <input autocomplete="off" type="text" name="shopcatId" readonly class="form-control" s-click="selectShopcat(this)"/>
                </td>
            </tr>
            <tr>
                <td>优选</td>
                <td>
                    <label class="s-checkbox-switch">
                        <input autocomplete="off" type="checkbox" name="recommend" value="true" />
                        <span class="s-checkbox-indicator"></span>
                    </label>
                </td>
            </tr>
            <tr>
                <td>抓取页数</td>
                <td>
                    <input autocomplete="off" type="number" name="maxPage" value="10" min="1" s-valid-error="require(),number()" class="form-control" />
                </td>
            </tr>
            <tr>
                <td>自动抓取</td>
                <td>
                    <label class="s-checkbox-switch">
                        <input autocomplete="off" type="checkbox" name="autoFetch" value="true" />
                        <span class="s-checkbox-indicator"></span>
                    </label>
                </td>
            </tr>
            <tr>
                <td>状态</td>
                <td>
                    <select class="form-control" s="select"
                            name="status"
                            s-build="S.get('/api/enum/valuesMap/core.enums.ItemSelectionStatus?igNames=DELETED')">
                    </select>
                </td>
            </tr>
            <tr>
                <td style="width: 100px;">类型</td>
                <td>
                    <select class="form-control" s="select"
                            id="typeSelect"
                            name="type"
                            s-build="S.get('/api/enum/valuesMap/core.enums.ItemSelectionType')">
                    </select>
                </td>
            </tr>
        </table>
        <input autocomplete="off" type="hidden" name="config" id="config" />
    </form>
    <div id="configPanel" s="window" class="form-inline">

    </div>
</div>
<script>
    if(itemSelection) {
        $form.node.attr("method", "put")
        $form.node.attr("action", "/api/itemSelection/" + itemSelection.id);
    }
    itemSelection =  itemSelection || {
        status: 'ACTIVE',
        type: 'TBK_DG_MATERIAL_OPTIONAL',
        maxPage: 10
    }
    itemSelection.name = name || itemSelection.name
    if(shopcat) {
        itemSelection.shopcatId = shopcat.id
    }
    S.meta.btns = [
        {
            name: "确定",
            style: "btn btn-primary",
            click: function() {
                $configPanel.getConfig().done(function(rs) {
                    $config.node.val(JSON.stringify(rs));
                    $form.submit().done(function() {
                        S.close(true)
                    })
                })
            }
        },
        {
            name: "关闭",
            style: "btn btn-default",
            click: function() {
                S.close();
            }
        }
    ]

    $typeSelect.node.change(function () {
        var val = $(this).val();
        var _config = $.extend(JSON.parse(itemSelection.config || '{}'), config || {});
        $configPanel.load("/itemSelection/" + val + ".html", {config: _config, catName: (shopcat || {}).title || ''})

    })

    S.on("s-ready", function () {
        $typeSelect.node.change();
    })

    function titleFn(obj) {
        return obj.favoritesTitle + (obj.type === 2 ? " - 高佣" : '');
    }

    function valFn(obj) {
        return obj.favoritesId
    }

    function sourceChange($this) {
        var data = $this.getSelectData();
        $name.node.val(data && data.favoritesTitle);
    }

    function selectShopcat($this) {
        S.popupOpen("/shopcat/shopcatSelector.html").done(function(shopcats, path) {
            path = path.concat(shopcats)
            if(shopcats && shopcats.length) {
                var id = shopcats[0].id
                $this.node.val(id);
                if($name.node.val() == '') {
                    $name.node.val(path.map(function(cat) {
                        return cat.title
                    }).join("-"))
                }
            }
        })
    }

</script>